package com.example.controller;

import com.example.model.User;
import com.example.service.impl.UserService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Api(value = "users",description = "Operations about users")
@RequestMapping(value = "/users")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取用户列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", defaultValue = "0", paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "一页显示的数量", defaultValue = "20", paramType = "query",dataType = "int")
    })
    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public List<User> getUserList(@RequestParam("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        System.out.println(currentPage);
        return userService.selectAll();
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public int registeUser(@RequestBody User user) {
        return userService.registerUser(user);
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUserById(@PathVariable("id") int id) {
        return userService.findUserById(id);
    }

    @ApiOperation(value = "登录", notes = "登录获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账户", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/session", method = RequestMethod.GET)
    public User getUserByAccount(@RequestParam("account") String account, @RequestParam("password") String password) {
        return userService.session(account, password);
    }

    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public int putUser(@PathVariable("id") int id, @RequestBody User user) {
        return userService.updateByIdSelective(user);
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public int deleteUser(@PathVariable("id") int id) {
        return userService.deleteByPrimaryKey(id);
    }

}
